"""
# -*- coding: utf-8 -*-
# @Time    : 2023/6/14 11:22
# @Author  : 王摇摆
# @FileName: Dataframe.py
# @Software: PyCharm
# @Blog    ：https://blog.csdn.net/weixin_44943389?type=blog
"""
# 导入工具库
import joblib
import pandas as pd
import xgboost as xgb
from sklearn.model_selection import train_test_split

# 用pandas读入数据
data = pd.read_csv('../data/pima-indians-diabetes.csv')
print('1. 数据集加载完成')

# 做数据切分
train, test = train_test_split(data)
feature_columns = ['Pregnancies', 'Glucose', 'BloodPressure', 'SkinThickness', 'Insulin', 'BMI',
                   'DiabetesPedigreeFunction', 'Age']
target_column = 'Outcome'
print('2. 数据集处理完成')

# 初始化模型
xgb_classifier = xgb.XGBClassifier(n_estimators=20, \
                                   max_depth=4, \
                                   learning_rate=0.1, \
                                   subsample=0.7, \
                                   colsample_bytree=0.7, \
                                   eval_metric='error')
print('3. 模型初始化成功')

# Dataframe格式数据拟合模型
xgb_classifier.fit(train[feature_columns], train[target_column])
print('4. 模型学习完毕')

# 使用模型预测
preds = xgb_classifier.predict(test[feature_columns])
print('5. 模型推理完毕')

# 判断准确率
error_rate = (preds != test[target_column]).mean()
print('错误率为%.2f%%' % (error_rate * 100))

# 模型存储
joblib.dump(xgb_classifier, './model/0003.model')
print('6. 模型推理完毕')

